//
//  ScrollViewCell.m
//  AmazingShopping
//
//  Created by Tan on 4/19/13.
//  Copyright (c) 2013 Tan Hoang. All rights reserved.
//

#import "ScrollViewCell.h"
#import "ProductDetailViewController.h"
#import "HomeViewController.h"

@implementation ScrollViewCell

@synthesize name, nameLabel, imageView, imagePath, viewButton, productID, price, priceLabel;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {        
        if (imageView == nil) {
            imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, frame.size.width-10, 130*frame.size.height/200)];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [self addSubview:imageView];
        }
        if (nameLabel == nil) {
            nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 130*frame.size.height/200, frame.size.width-10, 45*frame.size.height/200)];
            [nameLabel setNumberOfLines:3];
            [nameLabel setLineBreakMode:NSLineBreakByTruncatingTail];
            [nameLabel setTextAlignment:NSTextAlignmentCenter];
            [nameLabel setFont:[UIFont fontWithName:@"Helvetica" size:12]];
            [self addSubview:nameLabel];
        }
        if (priceLabel == nil) {
            priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 180*frame.size.height/200, frame.size.width-10, 25*frame.size.height/200)];
            [priceLabel setNumberOfLines:1];
            [priceLabel setTextAlignment:NSTextAlignmentCenter];
            [priceLabel setFont: [UIFont fontWithName:@"Helvetica" size:13]];
            [priceLabel setTextColor:[UIColor colorWithRed:170/255.0 green:10/255.0 blue:0/255.0 alpha:1.0]];
            [self addSubview:priceLabel];
        }
        if (viewButton == nil) {
            viewButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [viewButton setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
            [viewButton addTarget:self action:@selector(viewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:viewButton];
        }
        
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)setName:(NSString *)n{
    if (![n isEqualToString:name]) {
        name = [n copy];
        nameLabel.text = name;
    }
}

-(void)setImagePath:(NSString *)ip{
    if (![ip isEqualToString:imagePath]) {
        imagePath = [ip copy];
        NSData *imageData = [[NSData alloc]initWithContentsOfURL: [NSURL URLWithString:imagePath]];
        [imageView setImage:[UIImage imageWithData:imageData]];
    }
}

-(void)setProductID:(NSInteger *)pid{
    if (pid != productID) {
        productID = pid;
    }
}

-(void)setPrice:(NSString *)p{
    if (![price isEqualToString:p]) {
        price = [p copy];
        priceLabel.text = [NSString stringWithFormat:@"$%@", price];
    }
}

- (IBAction)viewButtonClicked:(id)sender {
    [self.delegate didSelectProduct:productID];
}
@end
